package com.hue.springcloud.controller;

import com.hue.springcloud.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@DefaultProperties(defaultFallback = "defaultMethod")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/consumer/payment/hystrix/ok")
    public String ok(){
        return orderService.ok();
    }

    @GetMapping("/consumer/payment/hystrix/timeout")
    @HystrixCommand(fallbackMethod = "timeoutHander",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
            })
    public String timeout() throws InterruptedException {
        return orderService.timeout();
    }

    @GetMapping("/consumer/payment/hystrix/testDefault")
    @HystrixCommand //指定异常时启用服务降级，但是没有指明方法，就调用默认的服务降级方法
    public String testFault(){
        int x=10/0;
        return "xxx";
    }

    public String timeoutHander(){
        System.out.println("80端口发生服务降级");
        return "80端口发生服务降级";
    }

    //指定要使用服务降级但却没有指明方法的时候会自动找到指定的默认方法来完成服务降级
    public String defaultMethod(){
        return "默认的服务降级";
    }
}
